package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.core.util.PageUtil;
import com.hbnu.system.mapper.ReportMapper;
import com.hbnu.system.model.context.BaseContext;
import com.hbnu.system.model.dto.CheckBackDTO;
import com.hbnu.system.model.dto.ReportAddDTO;
import com.hbnu.system.model.dto.SearchPageDTO;
import com.hbnu.system.model.dto.UserDTO;
import com.hbnu.system.model.entity.*;
import com.hbnu.system.model.vo.InfoVO;
import com.hbnu.system.model.vo.ReportVO;
import com.hbnu.system.service.*;
import com.hbnu.system.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {

    @Autowired
    IUserService iUserService;
    @Autowired
    IDeptService iDeptService;
    @Autowired
    IReportService iReportService;
    @Autowired
    IAuthorityService iAuthorityService;
    @Autowired
    AuditService auditService;

    @Autowired
    private IReportQualificationService iReportQualificationService;

    @Override
    public boolean addReport(Report report) {

        Report one1 = super.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportName, report.getReportName()));
        if(Objects.nonNull(one1) && !(one1.getReportID().equals(report.getReportID()))){
            throw new BaseException("报告名称重复!");
        }
//        paper.setStatus("待院级审核");
        report.setEnteredDate(LocalDateTime.now());

        return baseMapper.updateById(report) == 1;
    }

    @Override
    public boolean addReport(ReportAddDTO reportAddDTO) {
        if (StrUtil.isEmpty(reportAddDTO.getClassifi()) || StrUtil.isBlank(reportAddDTO.getClassifi())){
            throw new BaseException("请输入统计分类!");
        }
        if (StrUtil.isEmpty(reportAddDTO.getOrganization()) || StrUtil.isBlank(reportAddDTO.getOrganization())){
            throw new BaseException("请输入所属机构!");
        }
        if (StrUtil.isEmpty(reportAddDTO.getReportName()) || StrUtil.isBlank(reportAddDTO.getReportName())){
            throw new BaseException("请输入报告名!");
        }
        if (StrUtil.isEmpty(reportAddDTO.getLeadAuthor()) || StrUtil.isBlank(reportAddDTO.getLeadAuthor())){
            throw new BaseException("请输入负责人!");
        }
        if (StrUtil.isEmpty(reportAddDTO.getAdoptUnit()) || StrUtil.isBlank(reportAddDTO.getAdoptUnit())){
            throw new BaseException("请输入采纳单位");
        }
        if (StrUtil.isEmpty(reportAddDTO.getReportUnit()) || StrUtil.isBlank(reportAddDTO.getReportUnit())){
            throw new BaseException("请输入报告撰写人单位");
        }
        if (StrUtil.isEmpty(reportAddDTO.getReportOrigin()) || StrUtil.isBlank(reportAddDTO.getReportOrigin())){
            throw new BaseException("请输入研究报告来源");
        }
        if (StrUtil.isEmpty(reportAddDTO.getSurveyCate()) || StrUtil.isBlank(reportAddDTO.getSurveyCate())){
            throw new BaseException("请输入咨询调研类别");
        }
        Report report = new Report();


        report.setEntered(reportAddDTO.getEntered());
        report.setClassifi(reportAddDTO.getClassifi());
        report.setOrganization(reportAddDTO.getOrganization());
        report.setReportName(reportAddDTO.getReportName());
        report.setLeadAuthor(reportAddDTO.getLeadAuthor());
        report.setAdoptUnit(reportAddDTO.getAdoptUnit());
        report.setReportUnit(reportAddDTO.getReportUnit());
        report.setReportOrigin(reportAddDTO.getReportOrigin());
        report.setSurveyCate(reportAddDTO.getSurveyCate());

        String dept = iUserService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getName, report.getEntered())).getDept();
        String deptName = iDeptService.getById(dept).getDeptName();
        report.setDept(deptName);
        report.setStatus("草稿");
        report.setEnteredDate(LocalDateTime.now());
        return baseMapper.insert(report) == 1;
    }

    @Override
    public boolean deleteReport(String id) {
        Report one = super.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportID,   id));
        if (Objects.isNull(one)){
            throw new BaseException("报告编号有误!");
        }
        Report_Qualification report_qualification = iReportQualificationService.getOneReport_Qualification(id).get(0);
        if(Objects.nonNull(report_qualification)){
            iReportQualificationService.deleteReport_Qualification(report_qualification.getReportQualificationID());
        }

        return baseMapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateReport(Report report) {
        if (StrUtil.isEmpty(report.getClassifi()) || StrUtil.isBlank(report.getClassifi())){
            throw new BaseException("请输入统计分类!");
        }
        if (StrUtil.isEmpty(report.getOrganization()) || StrUtil.isBlank(report.getOrganization())){
            throw new BaseException("请输入所属机构!");
        }
        if (StrUtil.isEmpty(report.getReportName()) || StrUtil.isBlank(report.getReportName())){
            throw new BaseException("请输入报告名!");
        }
        if (StrUtil.isEmpty(report.getLeadAuthor()) || StrUtil.isBlank(report.getLeadAuthor())){
            throw new BaseException("请输入负责人!");
        }
        if (StrUtil.isEmpty(report.getAdoptUnit()) || StrUtil.isBlank(report.getAdoptUnit())){
            throw new BaseException("请输入采纳单位");
        }
        if (StrUtil.isEmpty(report.getReportUnit()) || StrUtil.isBlank(report.getReportUnit())){
            throw new BaseException("请输入报告撰写人单位");
        }
        if (StrUtil.isEmpty(report.getReportOrigin()) || StrUtil.isBlank(report.getReportOrigin())){
            throw new BaseException("请输入研究报告来源");
        }
        if (StrUtil.isEmpty(report.getSurveyCate()) || StrUtil.isBlank(report.getSurveyCate())){
            throw new BaseException("请输入咨询调研类别");
        }
        return baseMapper.updateById(report)==1;
    }

    @Override
    public Report getReport(String ReportID) {
        Report report = super.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportID, ReportID));
        if(Objects.isNull(report)){
            throw new BaseException("报告编号异常!未查找到该报告");
        }
        return report;
    }

    @Override
    public boolean checkReportName(String reportname) {
        Report one = super.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportName, reportname));
        if(Objects.nonNull(one)){
            throw new BaseException("该报告名称已存在!");
        }
        return true;
    }

    @Override
    public IPage<Report> pageCondition(SearchPageDTO searchPageDTO) {
        IPage<Report> page = PageUtil.getPage(searchPageDTO.getPageNum(), searchPageDTO.getPageSize());
        LambdaQueryWrapper<Report> wrapper = Wrappers.lambdaQuery(Report.class);
        IPage<Report> reportIPage = baseMapper.selectPage(page, wrapper);
//        IPage<PaperVO> taskVOIPage = iBeanService.copyPageProperties(paperIPage, PaperVO.class);
        List<Report> records = reportIPage.getRecords();
        List<Report> PaperS = new ArrayList<>();

        int count = 1;
        int start = searchPageDTO.getPageSize() * (searchPageDTO.getPageNum()-1)+1;
        int end = searchPageDTO.getPageNum() * searchPageDTO.getPageSize();
//        records.forEach(record->{
//            if (count >= start && count <= end){
//                PaperS.add(this.getPaper(record.getPaperID()));
//            }
//        });
        Collections.reverse(records);
        for (Report record:records) {
            if (count >= start && count <= end){
                PaperS.add(this.getReport(record.getReportID()));
            }
            count += 1;
        }


//        Collections.reverse(PaperS);
        reportIPage.setRecords(PaperS);
        reportIPage.setTotal(records.size());
        return reportIPage;
    }

    @Override
    public ReportVO getReportVO(String id) {
        return null;
    }

    @Override
    public Boolean collegeCheckPass(String token,String PID) {
        Report report = iReportService.getReport(PID);
        if (Objects.isNull(report)){
            throw new BaseException("该报告编号有误!");
        }
        if(!report.getStatus().equals("待院级审核")){
            throw new BaseException("该报告状态不处于该环节!");
        }

        InfoVO info = JwtUtils.getInfo(token);
        String[] strings = new String[1];
        strings[0] = iAuthorityService.getById(info.getRoles()[0]).getText();
        User user = iUserService.getById(info.getUid());
        String role = strings[0];
        String name = user.getName();

        Audit audit = new Audit();
        audit.setSynthesisID(PID);
        audit.setAuditerRole(role);
        audit.setAuditerName(name);

        if(role.equals("19185212210")){
            audit.setStatusChange("报告待院级审核->院级审核通过");
            report.setStatus("待校级审核");
        }else if(role.equals("19190251173")){
            audit.setStatusChange("报告待院级审核->校级审核通过");
            report.setStatus("审核通过");
        }
        audit.setAuditOpinion("通过");
        audit.setAuditerTime(LocalDateTime.now());
        auditService.addAudit(audit);
        return baseMapper.updateById(report) == 1;
    }

    @Override
    public Boolean collegeCheckBack(String token,CheckBackDTO checkBackDTO) {
        String id = checkBackDTO.getID();
        Report report = iReportService.getReport(id);
        if (Objects.isNull(report)){
            throw new BaseException("该报告编号有误!");
        }
        if(!report.getStatus().equals("待院级审核")){
            throw new BaseException("该报告状态不处于该环节!");
        }

        report.setReason(checkBackDTO.getReason());
        InfoVO info = JwtUtils.getInfo(token);
        String[] strings = new String[1];
        strings[0] = iAuthorityService.getById(info.getRoles()[0]).getText();
        User user = iUserService.getById(info.getUid());
        String role = strings[0];
        String name = user.getName();

        Audit audit = new Audit();
        audit.setSynthesisID(checkBackDTO.getID());
        audit.setAuditOpinion(checkBackDTO.getReason());
        audit.setAuditerRole(role);
        audit.setAuditerName(name);

        if(role.equals("19185212210")){
            audit.setStatusChange("报告待院级审核->被院级退回");
            report.setStatus("被院级退回");
        }else if(role.equals("19190251173")){
            audit.setStatusChange("报告待院级审核->被校级退回");
            report.setStatus("被校级退回");
        }
        audit.setAuditerTime(LocalDateTime.now());
        auditService.addAudit(audit);
        return baseMapper.updateById(report) == 1;
    }

    @Override
    public Boolean schoolCheckPass(String token,String PID) {
        Report report= iReportService.getReport(PID);
        if (Objects.isNull(report)){
            throw new BaseException("该报告编号有误!");
        }
        if(!report.getStatus().equals("待校级审核")){
            throw new BaseException("该报告状态不处于该环节!");
        }
        report.setStatus("审核通过");
        InfoVO info = JwtUtils.getInfo(token);
        String[] strings = new String[1];
        strings[0] = iAuthorityService.getById(info.getRoles()[0]).getText();
        User user = iUserService.getById(info.getUid());
        String role = strings[0];
        String name = user.getName();

        Audit audit = new Audit();
        audit.setSynthesisID(PID);
        audit.setAuditerRole(role);
        audit.setAuditerName(name);
        audit.setStatusChange("报告待校级审核->校级审核通过");
        audit.setAuditOpinion("通过");
        audit.setAuditerTime(LocalDateTime.now());
        auditService.addAudit(audit);
        return baseMapper.updateById(report) == 1;
    }

    @Override
    public Boolean schoolCheckBack(String token,CheckBackDTO checkBackDTO) {
        String id = checkBackDTO.getID();
        Report report = iReportService.getReport(id);
        if (Objects.isNull(report)){
            throw new BaseException("该报告编号有误!");
        }
        if(!report.getStatus().equals("待校级审核")){
            throw new BaseException("该报告状态不处于该环节!");
        }
        report.setStatus("被校级退回");
        report.setReason(checkBackDTO.getReason());
        InfoVO info = JwtUtils.getInfo(token);
        String[] strings = new String[1];
        strings[0] = iAuthorityService.getById(info.getRoles()[0]).getText();
        User user = iUserService.getById(info.getUid());
        String role = strings[0];
        String name = user.getName();

        Audit audit = new Audit();
        audit.setSynthesisID(checkBackDTO.getID());
        audit.setAuditOpinion(checkBackDTO.getReason());
        audit.setAuditerRole(role);
        audit.setAuditerName(name);
        audit.setStatusChange("报告待校级审核->被校级退回");
        audit.setAuditerTime(LocalDateTime.now());
        auditService.addAudit(audit);
        return baseMapper.updateById(report) == 1;
    }

    @Override
    public Boolean withdrawReport(String id) {
        // 验证报告存在
        Report report = super.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportID,id));
        if(Objects.isNull(report)){
            throw new BaseException("报告编号有误!");
        }
        
        // 检查报告状态是否为"待院级审核"
        if(!report.getStatus().equals("待院级审核")){
            throw new BaseException("该报告状态不处于院级审核中，无法撤回!");
        }
        
        // 获取当前用户信息，验证权限
        String userId = BaseContext.getCurrentId();
        User user = iUserService.getById(userId);
        if (user == null) {
            throw new BaseException("用户不存在");
        }
        
        // 将状态更新为草稿
        report.setStatus("草稿");
        report.setReason("");
        
        return baseMapper.updateById(report) == 1;
    }
    
    @Override
    public Boolean ReCommitReport(String id) {
        Report report = super.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportID,id));
        if(Objects.isNull(report)){
            throw new BaseException("报告编号有误!");
        }
        if(!report.getStatus().equals("草稿") && !report.getStatus().equals("被院级退回") && !report.getStatus().equals("被校级退回")){
            throw new BaseException("该论文不适用于该阶段!");
        }
        //检查各项不可为空选项
        if (StrUtil.isEmpty(report.getClassifi()) || StrUtil.isBlank(report.getClassifi())){
            throw new BaseException("请输入统计分类!");
        }
        if (StrUtil.isEmpty(report.getOrganization()) || StrUtil.isBlank(report.getOrganization())){
            throw new BaseException("请输入所属机构!");
        }
        if (StrUtil.isEmpty(report.getReportName()) || StrUtil.isBlank(report.getReportName())){
            throw new BaseException("请输入报告名!");
        }
        if (StrUtil.isEmpty(report.getLeadAuthor()) || StrUtil.isBlank(report.getLeadAuthor())){
            throw new BaseException("请输入负责人!");
        }
        if (StrUtil.isEmpty(report.getAdoptUnit()) || StrUtil.isBlank(report.getAdoptUnit())){
            throw new BaseException("请输入采纳单位");
        }
        if (StrUtil.isEmpty(report.getReportUnit()) || StrUtil.isBlank(report.getReportUnit())){
            throw new BaseException("请输入报告撰写人单位");
        }
        if (StrUtil.isEmpty(report.getReportOrigin()) || StrUtil.isBlank(report.getReportOrigin())){
            throw new BaseException("请输入研究报告来源");
        }
        if (StrUtil.isEmpty(report.getSurveyCate()) || StrUtil.isBlank(report.getSurveyCate())){
            throw new BaseException("请输入咨询调研类别");
        }
        report.setStatus("待院级审核");
        report.setReason("");
//        if (StrUtil.isEmpty(paper.getStatus()) || StrUtil.isBlank(paper.getStatus())){
//            throw new BaseException("请将其保存为草稿或提交!");
//        }
        return baseMapper.updateById(report)==1;
    }

    @Override
    public IPage<Report> userGetRelateReport(UserDTO userDTO) {
        User user = iUserService.getOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getName, userDTO.getName()));
        if(Objects.isNull(user)){
            throw new BaseException("用户姓名有误!");
        }

        IPage<Report> page = PageUtil.getPage(userDTO.getPageNum(), userDTO.getPageSize());

        LambdaQueryWrapper<Report> wrapper = new LambdaQueryWrapper<>();
        if(user.getRole().equals("19177623324")){//判断身份如果是普通用户的话
            if(StrUtil.isBlank(userDTO.getState()) || StrUtil.isEmpty(userDTO.getState())){
                wrapper = Wrappers.lambdaQuery(Report.class)
                        .eq(Report::getEntered, userDTO.getName())
                        .or().eq(Report::getLeadAuthor,userDTO.getName())
                        .or().like(Report::getAllAuthor, userDTO.getName());
            }else {
                wrapper = Wrappers.lambdaQuery(Report.class)
                        .eq(Report::getStatus, userDTO.getState())
                        .eq(Report::getEntered, userDTO.getName())
                        .or().eq(Report::getLeadAuthor,userDTO.getName())
                        .or().like(Report::getAllAuthor, userDTO.getName());
            }

        }else if (user.getRole().equals("19185212210")){ // 如果是院级管理员 给他看本院的
            if(StrUtil.isBlank(userDTO.getState()) || StrUtil.isEmpty(userDTO.getState())){
                Dept dept = iDeptService.getById(user.getDept());
                wrapper = Wrappers.lambdaQuery(Report.class)
                        .eq(Report::getDept,dept.getDeptName());
            }else {
                Dept dept = iDeptService.getById(user.getDept());
                wrapper = Wrappers.lambdaQuery(Report.class)
                        .eq(Report::getDept,dept.getDeptName())
                        .eq(Report::getStatus, userDTO.getState());
            }

        }else { // 校级管理员 看所有  给校级多提供一个字段——院系名字
            if(StrUtil.isEmpty(userDTO.getDept()) || StrUtil.isBlank(userDTO.getDept())){
                if(StrUtil.isBlank(userDTO.getState()) || StrUtil.isEmpty(userDTO.getState())){
                    wrapper = Wrappers.lambdaQuery(Report.class);
                }else {
                    wrapper = Wrappers.lambdaQuery(Report.class)
                            .eq(Report::getStatus, userDTO.getState());
                }

            }else {
                if(StrUtil.isBlank(userDTO.getState()) || StrUtil.isEmpty(userDTO.getState())){
                    wrapper = Wrappers.lambdaQuery(Report.class)
                            .eq(Report::getDept,userDTO.getDept());
                }else {
                    wrapper = Wrappers.lambdaQuery(Report.class)
                            .eq(Report::getDept,userDTO.getDept())
                            .eq(Report::getStatus, userDTO.getState());
                }

            }
        }
        IPage<Report> reportIPage = baseMapper.selectPage(page, wrapper);
        List<Report> records = reportIPage.getRecords();
//        IPage<PaperVO> paperVOIPage = iBeanService.copyPageProperties(paperIPage, PaperVO.class);
        ArrayList<Report> paperList = new ArrayList<>();

        //对分页的设置
        int count = 1;
        int start = userDTO.getPageSize() * (userDTO.getPageNum()-1)+1;
        int end = userDTO.getPageNum() * userDTO.getPageSize();
        Collections.reverse(records);
        for (Report record:records) {
            if (count >= start && count <= end){
                paperList.add(this.getReport(record.getReportID()));
//                paperList.add(this.getPaperVO(record.getPaperID()));
            }
            count += 1;
        }

//        Collections.reverse(paperList);
        reportIPage.setRecords(paperList);
        reportIPage.setTotal(records.size());

        return reportIPage;
    }

    @Override
    public float[] getFigure(UserDTO userDTO) {
        float[] result = new float[5];
        User user = iUserService.getOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getName, userDTO.getName()));

        LambdaQueryWrapper<Report> wrapper = new LambdaQueryWrapper<>();
        if(user.getRole().equals("19177623324")){//判断身份如果是普通用户的话
            if(StrUtil.isBlank(userDTO.getState()) || StrUtil.isEmpty(userDTO.getState())){
                wrapper = Wrappers.lambdaQuery(Report.class)
                        .eq(Report::getEntered, userDTO.getName())
                        .or().eq(Report::getLeadAuthor,userDTO.getName())
                        .or().like(Report::getAllAuthor, userDTO.getName());
            }else {
                wrapper = Wrappers.lambdaQuery(Report.class)
                        .eq(Report::getStatus, userDTO.getState())
                        .eq(Report::getEntered, userDTO.getName())
                        .or().eq(Report::getLeadAuthor,userDTO.getName())
                        .or().like(Report::getAllAuthor, userDTO.getName());
            }

        }else if (user.getRole().equals("19185212210")){ // 如果是院级管理员 给他看本院的
            if(StrUtil.isBlank(userDTO.getState()) || StrUtil.isEmpty(userDTO.getState())){
                Dept dept = iDeptService.getById(user.getDept());
                wrapper = Wrappers.lambdaQuery(Report.class)
                        .eq(Report::getDept,dept.getDeptName());
            }else {
                Dept dept = iDeptService.getById(user.getDept());
                wrapper = Wrappers.lambdaQuery(Report.class)
                        .eq(Report::getDept,dept.getDeptName())
                        .eq(Report::getStatus, userDTO.getState());
            }

        }else { // 校级管理员 看所有  给校级多提供一个字段——院系名字
            if(StrUtil.isEmpty(userDTO.getDept()) || StrUtil.isBlank(userDTO.getDept())){
                if(StrUtil.isBlank(userDTO.getState()) || StrUtil.isEmpty(userDTO.getState())){
                    wrapper = Wrappers.lambdaQuery(Report.class);
                }else {
                    wrapper = Wrappers.lambdaQuery(Report.class)
                            .eq(Report::getStatus, userDTO.getState());
                }

            }else {
                if(StrUtil.isBlank(userDTO.getState()) || StrUtil.isEmpty(userDTO.getState())){
                    wrapper = Wrappers.lambdaQuery(Report.class)
                            .eq(Report::getDept,userDTO.getDept());
                }else {
                    wrapper = Wrappers.lambdaQuery(Report.class)
                            .eq(Report::getDept,userDTO.getDept())
                            .eq(Report::getStatus, userDTO.getState());
                }

            }
        }
        List<Report> reportList = baseMapper.selectList(wrapper);

        for (Report report : reportList) {
            result[0] += report.getScore();
        }
        return result;
    }

    @Override
    public IPage<Report> collegeUserGetReport(UserDTO userDTO) {
        User one = iUserService.getOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getName, userDTO.getName()));
        if(Objects.isNull(one)){
            throw new BaseException("用户姓名有误!");
        }
        String dept = one.getDept();
        String deptName = iDeptService.getById(dept).getDeptName();
        String role = one.getRole();
        if(!role.equals("19185212210")&&!role.equals("19190251173")){
            throw new BaseException("该用户权限不可访问!");
        }
        IPage<Report> page = PageUtil.getPage(userDTO.getPageNum(), userDTO.getPageSize());
        LambdaQueryWrapper<Report> wrapper = Wrappers.lambdaQuery(Report.class)
                .eq(Report::getDept,deptName)
                .and(wrapper1 -> wrapper1.eq(Report::getStatus,"待院级审核"));

        IPage<Report> reportIPage = baseMapper.selectPage(page, wrapper);
        List<Report> records = reportIPage.getRecords();
//        IPage<PaperVO> paperVOIPage = iBeanService.copyPageProperties(paperIPage, PaperVO.class);
        ArrayList<Report> paperList = new ArrayList<>();

        //对分页的设置
        int count = 1;
        int start = userDTO.getPageSize() * (userDTO.getPageNum()-1)+1;
        int end = userDTO.getPageNum() * userDTO.getPageSize();
        Collections.reverse(records);
        for (Report record:records) {
            if (count >= start && count <= end){
//                this.getPaper(record.getPaperID())
                paperList.add(this.getReport(record.getReportID()));
            }
            count += 1;
        }

//        Collections.reverse(paperList);
        reportIPage.setRecords(paperList);
        reportIPage.setTotal(records.size());

        return reportIPage;
    }

    @Override
    public IPage<Report> schoolUserGetReport(UserDTO userDTO) {
        User one = iUserService.getOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getName, userDTO.getName()));
        if(Objects.isNull(one)){
            throw new BaseException("用户姓名有误!");
        }
        String dept = one.getDept();
        String deptName = iDeptService.getById(dept).getDeptName();
        String role = one.getRole();
        if(!role.equals("19190251173")){
            throw new BaseException("该用户权限不为校级审核员!");
        }
        IPage<Report> page = PageUtil.getPage(userDTO.getPageNum(), userDTO.getPageSize());
        LambdaQueryWrapper<Report> wrapper = Wrappers.lambdaQuery(Report.class)
//                .eq(Paper::getDept,deptName)
                .and(wrapper1 -> wrapper1.eq(Report::getStatus,"待校级审核"));

        IPage<Report> reportIPage = baseMapper.selectPage(page, wrapper);
        List<Report> records = reportIPage.getRecords();
//        IPage<PaperVO> paperVOIPage = iBeanService.copyPageProperties(paperIPage, PaperVO.class);
        ArrayList<Report> paperList = new ArrayList<>();

        //对分页的设置
        int count = 1;
        int start = userDTO.getPageSize() * (userDTO.getPageNum()-1)+1;
        int end = userDTO.getPageNum() * userDTO.getPageSize();
        Collections.reverse(records);
        for (Report record:records) {
            if (count >= start && count <= end){
//                this.getPaper(record.getPaperID())
                paperList.add(this.getReport(record.getReportID()));
            }
            count += 1;
        }

//        Collections.reverse(paperList);
        reportIPage.setRecords(paperList);
        reportIPage.setTotal(records.size());

        return reportIPage;
    }
}
